

PHYSICAL_OBJECT = {}

BOXES_WIDTH = 36
BOXES_HEIGHT = 36
--MAP_HEIGHT = 260
--MAP_WIDTH = 260

physic = {}

function physic.createPO(_map_width, _map_height, _boxe_width, _box_height )
	for i = 1, math.floor((_map_width/_boxe_width) + 2) do  -- checken ob for nicht eimal zu wenig aufgerufen wird
		PHYSICAL_OBJECT[i] = {}	
		for ii = 1,  math.floor((_map_height/ _box_height)+ 2) do 
			PHYSICAL_OBJECT[i][ii] = {}
		end
	end
end


function physic.addPO(object)
	x = math.abs(math.floor(object.center[1]/BOXES_WIDTH)) + 1
	y = math.abs(math.floor(object.center[2]/BOXES_HEIGHT)) + 1
	object:setBox(x,y)
	table.insert(PHYSICAL_OBJECT[x][y],object)
end


function physic.uppdatePO()
	tmp_stack_ ={}
	for i = 1, #PHYSICAL_OBJECT do 
		for ii = 1, #PHYSICAL_OBJECT[i] do
			if #PHYSICAL_OBJECT[i][ii] > 0 then
			c = 1
				while c <= #PHYSICAL_OBJECT[i][ii] do
					if PHYSICAL_OBJECT[i][ii] ~= nil then
						object = PHYSICAL_OBJECT[i][ii][c]
						if object.xBox ~=  math.floor(object.center[1]/BOXES_WIDTH) + 1 or object.yBox ~= math.floor(object.center[2]/BOXES_HEIGHT) + 1 then
							table.insert(tmp_stack_, object )
							table.remove(PHYSICAL_OBJECT[i][ii], c)
						else
							c = c + 1
						end
					else
						break
					end
				end
			end
		end
	end
	for i = 1, #tmp_stack_ do
		physic.addPO(tmp_stack_[i])
	end
end

function physic.movePO()
	for i = 1, #PHYSICAL_OBJECT do
		for ii = 1, #PHYSICAL_OBJECT[i] do
			for _, object in ipairs(PHYSICAL_OBJECT[i][ii]) do
				object:move (object.velocity[1], object.velocity[2])
			end		
		end
	end
end

function physic.collisionDetection() -- checks if 2 GE are not allowd to be on the same place, and replaces them if no allowd
	for i = 1, #PHYSICAL_OBJECT do
		for ii = 1, #PHYSICAL_OBJECT[i] do
			for _, object in ipairs(PHYSICAL_OBJECT[i][ii]) do
				if object.velocity[1] ~= 0 or object.velocity[2] ~= 0 then
					if not  physic.mapBorderCollision(object) then --collision with border
						object:move(-object.velocity[1],- object.velocity[2])
					end
					if not object.passable and physic.gameelementCollision(object) then -- collision with otehr GE
						object:move(-object.velocity[1],-object.velocity[2])
						
					end
				end
			end		
		end
	end
end

function physic.gameelementCollision(object) --checks for objct if there is a collision in with an element in PHYSICAL_OBJECT
	for i = object.xBox-1, object.xBox+1 do
		if PHYSICAL_OBJECT[i] then 
			for ii = object.yBox-1,object.yBox+1 do
				if PHYSICAL_OBJECT[i][ii] then 
					for _ ,object2 in ipairs(PHYSICAL_OBJECT[i][ii]) do
					
							if object.team ~= object2.team and not object2.passable and gameelementCollision(object, object2)  then return object2 end
					end
				end
			end
		end
	end
	return false
end

function physic.mapBorderCollision(_object) -- returns true as long as the GE is still in the map
	if (_object.center[1] - _object.width*_object.offsetWidth/2 <  0 or _object.center[2] - _object.height*_object.offsetHeight/2 < 0 or _object.center[1] + _object.width*_object.offsetWidth/2 > MAP_WIDTH  or _object.center[2] + _object.height*_object.offsetHeight/2 > MAP_HEIGHT) then
		return false
	else
		return true
	 end
end
